package pers.xiaojun.boot.module.system.controller.dict;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import pers.xiaojun.boot.mybatis.core.convert.PageConvert;
import pers.xiaojun.boot.common.enums.CommonStatusEnum;
import pers.xiaojun.boot.common.pojo.CommonResult;
import pers.xiaojun.boot.common.pojo.PageResult;
import pers.xiaojun.boot.common.util.BeanUtils;
import pers.xiaojun.boot.module.system.controller.dict.vo.data.DictDataQueryReqVO;
import pers.xiaojun.boot.module.system.controller.dict.vo.data.DictDataRespVO;
import pers.xiaojun.boot.module.system.controller.dict.vo.data.DictDataSaveRespVO;
import pers.xiaojun.boot.module.system.dao.entity.dict.DictDataDO;
import pers.xiaojun.boot.module.system.service.dict.DictDataService;
import pers.xiaojun.boot.web.core.annotation.AdminApi;

import java.util.List;

/**
 * 字典数据接口
 *
 * @author xiaojun
 * @since 2025-10-23
 */
@Tag(name = "字典数据")
@AdminApi
@RestController
@RequestMapping("/system/dict-data")
@RequiredArgsConstructor
public class DictDataController {

    private final DictDataService dictDataService;

    @PostMapping
    @Operation(summary = "创建字典数据")
    @PreAuthorize("@ss.hasPermission('system:dict:crdate')")
    public CommonResult<?> createDictData(@Valid @RequestBody DictDataSaveRespVO createVO) {
        return CommonResult.success(dictDataService.saveDictData(createVO).getId());
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新字典数据")
    @PreAuthorize("@ss.hasPermission('system:dict:update')")
    @Parameter(name = "id", description = "字典数据Id", required = true)
    public CommonResult<?> updateDictData(@PathVariable("id") Long id, @Valid @RequestBody DictDataSaveRespVO updateVO) {
        updateVO.setId(id);
        return CommonResult.success(dictDataService.updateDictData(updateVO).getId());
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除字典数据")
    @PreAuthorize("@ss.hasPermission('system:dict:delete')")
    @Parameter(name = "id", description = "字典数据Id", required = true)
    public CommonResult<?> deleteDictData(@PathVariable("id") Long deptDataId) {
        dictDataService.removeDictDataById(deptDataId);
        return CommonResult.success();
    }

    @DeleteMapping("/batch/{ids}")
    @Operation(summary = "删除字典数据")
    @PreAuthorize("@ss.hasPermission('system:dict:delete')")
    @Parameter(name = "ids", description = "字典数据Id列表", required = true)
    public CommonResult<?> deleteDictDataBatch(@PathVariable("ids") List<Long> deptDataIds) {
        dictDataService.removeDictDataByIds(deptDataIds);
        return CommonResult.success();
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取字典数据")
    @PreAuthorize("@ss.hasPermission('system:dict:query')")
    @Parameter(name = "id", description = "字典数据Id", required = true)
    public CommonResult<DictDataRespVO> getDictData(@PathVariable("id") Long deptDataId) {
        return CommonResult.success(BeanUtil.toBean(dictDataService.getById(deptDataId), DictDataRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获取字典分页数据")
    @PreAuthorize("@ss.hasPermission('system:dict:query')")
    public CommonResult<PageResult<DictDataRespVO>> getDictDataPage(@Valid DictDataQueryReqVO queryVO) {
        return CommonResult.success(PageConvert.INSTANCE.toPageResult(dictDataService.listDictDatasPage(queryVO), DictDataRespVO.class));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获取字典列表数据", description = "获取开启状态的字典列表数据")
    @PreAuthorize("@ss.hasPermission('system:dict:query')")
    public CommonResult<List<DictDataRespVO>> getSimpleDictDataList() {
        List<DictDataDO> DictDataList = dictDataService.listDictDatasByStatusAndDictType(CommonStatusEnum.ENABLE.getStatus(), null);
        return CommonResult.success(BeanUtils.toBeans(DictDataList, DictDataRespVO.class));
    }

}
